package com.hjq.demo.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.BaseDialog;
import com.hjq.base.FragmentPagerAdapter;
import com.hjq.demo.R;
import com.hjq.demo.aop.SingleClick;
import com.hjq.demo.app.AppFragment;
import com.hjq.demo.app.TitleBarFragment;
import com.hjq.demo.http.api.ShipmentListApi;
import com.hjq.demo.other.NewConfig;
import com.hjq.demo.ui.activity.AddCustomerInformationActivity;
import com.hjq.demo.ui.activity.AddShipmentRecordActivity;
import com.hjq.demo.ui.activity.BrowserActivity;
import com.hjq.demo.ui.activity.HomeActivity;
import com.hjq.demo.ui.activity.SearchCustomerRecordActivity;
import com.hjq.demo.ui.activity.SearchShipmentRecordActivity;
import com.hjq.demo.ui.activity.SettingActivity;
import com.hjq.demo.ui.adapter.TabAdapter;
import com.hjq.demo.ui.dialog.MessageDialog;
import com.hjq.demo.ui.popup.ListPopup;
import com.hjq.demo.widget.XCollapsingToolbarLayout;
import com.hjq.http.EasyLog;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 首页 Fragment
 */
public final class HomeFragment extends TitleBarFragment<HomeActivity>
        implements TabAdapter.OnTabListener, ViewPager.OnPageChangeListener,
        XCollapsingToolbarLayout.OnScrimsListener {

    private XCollapsingToolbarLayout mCollapsingToolbarLayout;
    private Toolbar mToolbar;

    private TextView mAddressView;
    private TextView mHintView;
    private AppCompatImageView mAddCustomerInformation;

    private RecyclerView mTabView;
    private ViewPager mViewPager;

    private TabAdapter mTabAdapter;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }
    public int mType = 0;
    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment;
    }

    @Override
    protected void initView() {
        mCollapsingToolbarLayout = findViewById(R.id.ctl_home_bar);
        mToolbar = findViewById(R.id.tb_home_title);

        mAddressView = findViewById(R.id.tv_home_address);
        mHintView = findViewById(R.id.tv_home_hint);
        mAddCustomerInformation = findViewById(R.id.iv_add_ic);

        mTabView = findViewById(R.id.rv_home_tab);
        mViewPager = findViewById(R.id.vp_home_pager);

        mPagerAdapter = new FragmentPagerAdapter<>(this);
//        mPagerAdapter.addFragment(StatusFragment.newInstance(), "列表演示");
        mPagerAdapter.addFragment(ShipmentRecordFragment.newInstance(), "出货记录");
        mPagerAdapter.addFragment(CustomerInformationRecordFragment.newInstance(), "客户信息记录");
//        mPagerAdapter.addFragment(BrowserFragment.newInstance("https://github.com/getActivity"), "网页演示");
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(this);

        mTabAdapter = new TabAdapter(getAttachActivity());
        mTabView.setAdapter(mTabAdapter);

        // 给这个 ToolBar 设置顶部内边距，才能和 TitleBar 进行对齐
        ImmersionBar.setTitleBar(getAttachActivity(), mToolbar);

        //设置渐变监听
        mCollapsingToolbarLayout.setOnScrimsListener(this);
        //点击事件
        setOnClickListener(mAddCustomerInformation,mHintView);
    }
    @Override
    protected void initData() {
        mTabAdapter.addItem("出货记录");
        mTabAdapter.addItem("客户信息记录");
        mTabAdapter.setOnTabListener(this);
    }
    @SingleClick
    @Override
    public void onClick(View view) {
        //添加客户
        if (view == mAddCustomerInformation) {
            // 菜单弹窗
            new ListPopup.Builder(getContext())
                    .setList("选择客户信息", "选择出货记录")
                    .addOnShowListener(popupWindow ->
                            EasyLog.print("PopupWindow 显示了")
                    )
                    .addOnDismissListener(popupWindow ->
                                    EasyLog.print("PopupWindow 销毁了")
                    )
                    .setListener((ListPopup.OnListener<String>) (popupWindow, position, s) -> {
                        if (s.equals("选择客户信息")){
                            mType = 1;
                            AddCustomerInformationActivity.start(getContext(),"2","","","",0);
                        }else if (s.equals("选择出货记录")){
                            mType = 1;
                            AddShipmentRecordActivity.start(getContext(),"1");
                        }

                    })
                    .showAsDropDown(view);
        }else if (view == mHintView){
            // 消息对话框
            new MessageDialog.Builder(getActivity())
                    // 标题可以不用填写
                    .setTitle("进入搜索页面")
                    // 内容必须要填写
                    .setMessage("是进入出货记录搜索还是客户信息搜索")
                    // 确定按钮文本
                    .setConfirm("出货记录搜索")
                    // 设置 null 表示不显示取消按钮
                    .setCancel("客户信息搜索")
                    // 设置点击按钮后不关闭对话框
                    //.setAutoDismiss(false)
                    .setListener(new MessageDialog.OnListener() {

                        @Override
                        public void onConfirm(BaseDialog dialo) {
                            mType = 1;
                            startActivity(SearchShipmentRecordActivity.class);
                        }
                        @Override
                        public void onCancel(BaseDialog dialog) {
                            mType = 1;
                            startActivity(SearchCustomerRecordActivity.class);
                        }
                    })
                    .show();
        }
    }
    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }

    @Override
    public boolean isStatusBarDarkFont() {
        return mCollapsingToolbarLayout.isScrimsShown();
    }

    /**
     * {@link TabAdapter.OnTabListener}
     */

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        mViewPager.setCurrentItem(position);
        return true;
    }

    /**
     * {@link ViewPager.OnPageChangeListener}
     */

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        if (mTabAdapter == null) {
            return;
        }
        mTabAdapter.setSelectedPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {}

    /**
     * CollapsingToolbarLayout 渐变回调
     *
     * {@link XCollapsingToolbarLayout.OnScrimsListener}
     */
    @SuppressLint("RestrictedApi")
    @Override
    public void onScrimsStateChange(XCollapsingToolbarLayout layout, boolean shown) {
        getStatusBarConfig().statusBarDarkFont(shown).init();
        mAddressView.setTextColor(ContextCompat.getColor(getAttachActivity(), shown ? R.color.black : R.color.white));
        mHintView.setBackgroundResource(shown ? R.drawable.home_search_bar_gray_bg : R.drawable.home_search_bar_transparent_bg);
        mHintView.setTextColor(ContextCompat.getColor(getAttachActivity(), shown ? R.color.black60 : R.color.white60));
        mAddCustomerInformation.setSupportImageTintList(ColorStateList.valueOf(getColor(shown ? R.color.common_icon_color : R.color.white)));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mViewPager.setAdapter(null);
        mViewPager.removeOnPageChangeListener(this);
        mTabAdapter.setOnTabListener(null);
    }
    public  void  initDataRf(){
        if (mType > 0){
            if (mPagerAdapter.getCount() > 1){
                ShipmentRecordFragment  shipmentRecordFragment = (ShipmentRecordFragment) mPagerAdapter.getItem(0);
                shipmentRecordFragment.initmRefreshLayout();
                CustomerInformationRecordFragment  customerInformationRecordFragment = (CustomerInformationRecordFragment) mPagerAdapter.getItem(1);
                customerInformationRecordFragment.initmRefreshLayout();
            }
        }
    }


}